var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        page: 1,
        listRows: 20,
        total: 0,
        gmt_start:'',
        gmt_end:'',
        loanSn:'',
        order:'desc',
        mobile:'',
        state:''
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page: _this.params.page,
            listRows: _this.params.listRows,
            loanSn:_this.params.loanSn,
            order:_this.params.order,
            gmt_end:_this.params.gmt_end,
            gmt_start:_this.params.gmt_start,
            state:_this.params.state,

        };
        console.log(paramsObj);
        Api.billlist(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.list);
            _this.params.total = data.totalPages;
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    index: 1,
                    theme: '#3498db',
                    curr: _this.params.page,
                    count: _this.params.total, //数据总数，从服务端得到
                    hash: 'curr_page',
                    layout: ['prev', 'page', 'next', 'skip'],
                    jump: function(obj, first) {
                        _this.params.page = obj.curr;
                        //obj包含了当前分页的所有参数，比如：curr当前页
                        var Params = {
                            passport: _this.params.passport,
                            page: _this.params.page,
                            listRows: _this.params.listRows,
                            loanSn:_this.params.loanSn,
                            order:_this.params.order,
                            gmt_end:_this.params.gmt_end,
                            gmt_start:_this.params.gmt_start,
                            state:_this.params.state,

                        };
                        Api.billlist(Params, function(data) {
                            _this.getDataList(data.list);


                        })
                    }
                });
            });
        });
    },
    getDataList: function(data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>' +

                        '<td class="text-c">' + data[i].loanSn + '</td>' +
                        '<td class="text-c">' + data[i].realName + '</td>' +
                        '<td class="text-c">' + data[i].gmtCreate + '</td>'+
                        '<td class="text-c">￥' + data[i].money + '</td>' +
                        '<td class="text-c">' + data[i].batch + '</td>'+
                        '<td class="text-c">￥' + data[i].unpaidMoney + '</td>';
                if (data[i].state==0) {
                    html+='<td class="text-c">还款中</td>';
                }else if(data[i].state==1){
                    html+='<td class="text-c">已还款</td>';
                }else if(data[i].state==2){
                    html+='<td class="text-c">已逾期</td>';
                };

                    html +='<td class="text-c">' +
                        '<p><a loanId="' + data[i].loanId + '" state="' + data[i].state + '"  loanSn="' + data[i].loanSn + '" class="handle view">查看订单</a><a loanId="' + data[i].loanId + '" state="' + data[i].state + '"  loanSn="' + data[i].loanSn + '" class="handle delete">删除</a></p>' +
                        '</td>' +
                        '</tr>';
            }
            $('#List').html(html);
            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="8" >暂无数据</td>' +
                '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent: function() {
        var _this = this;
        $('.view').on('click', function() {
            Pub.setSession('loanId', $(this).attr('loanId'));
            Pub.setSession('loanSn', $(this).attr('loanSn'));
            Pub.setSession('state', $(this).attr('state'));
            window.location.href = Pub.getHtmlUrl('./finance-bill-detail.html');
        });
        $('.delete').on('click', function(event) {
            event.preventDefault();
            var loanId=$(this).attr('loanId');
            layui.use('layer', function(){
                layer.confirm('您确定要删除吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(index){
                    _this.deleteEvent(loanId);
                    layer.close(index);
                }, function(){

                });
            });
        });
    },
    deleteEvent:function(loanId){
        var _this = this;
        var Params = {
            passport: _this.params.passport,
            loanId:loanId
        };
        console.log(Params);
        Api.operationalinformationlistdelete(Params, function(data) {
            _this.params.page=1;
            _this.getData();

        })
    },
    otherEvent:function(){
        var _this = this;
        layui.use('laydate', function() {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#time',
                range: '~',
                done: function(value, date, endDate) {
                    console.log(date);
                    if (date.year) {
                        if (date.month < 10) {
                            date.month = "0" + date.month;
                        }
                        if (date.date < 10) {
                            date.date = "0" + date.date;
                        }
                        if (endDate.month < 10) {
                            endDate.month = "0" + endDate.month;
                        }
                        if (endDate.date < 10) {
                            endDate.date = "0" + endDate.date;
                        }
                        var gmt_start = date.year + '-' + date.month + '-' + date.date + ' 00:00:00';
                        var gmt_end = endDate.year + '-' + endDate.month + '-' + endDate.date + ' 23:59:59';
                        _this.params.gmt_start = gmt_start;
                        _this.params.gmt_end = gmt_end;

                    } else {
                        _this.params.gmt_start = '';
                        _this.params.gmt_end = '';
                    };
                }
            });
        });
        $('#normalSearch').click(function(event) {
            _this.params.loanSn=$('#loanSn').val();

            _this.params.page=1;
            _this.getData();
        });
        $('.slectShow').click(function(event) {
            $('.select-wrap').toggle(400);
        });
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('#stateSelect').on('change', function(event) {
            event.preventDefault();
            _this.params.state = $(this).val();
        });
        $('#changeSize').on('change', function(event) {
            event.preventDefault();
            _this.params.listRows = $(this).val();
            _this.params.page=1;
            $('#page').html('');
            _this.getData();
        });
        $('#changeDesc').on('change', function(event) {
            event.preventDefault();
            if ($(this).val() !== '排序方式') {
                _this.params.order = $(this).val();
                _this.params.page=1;
                $('#page').html('');
                _this.getData();
            };
        });

    },
    initEvent: function(){
        var _this = this;
        _this.getData();
        _this.otherEvent();


    }
};
var page = new Page();